package 案例;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
class Student{
    public int studentID;
    public int classID;
}
@WebServlet("/queryInfo")
public class queryStudentInfo extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String query=req.getQueryString();
        System.out.println(query);
        String classId=req.getParameter("classId");
        String studentId=req.getParameter("studentId");
        resp.getWriter().write("classId:"+classId+" studentId:"+studentId);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        ①通过x-www-form-urlencoded解析
        //doGet(req,resp);
//        ②通过json格式解析
        ObjectMapper objectMapper=new ObjectMapper();
        Student student=objectMapper.readValue(req.getInputStream(),Student.class);
        System.out.println("classID:"+student.classID+" studentID:"+student.studentID);
        resp.getWriter().write("classID:"+student.classID+" studentID:"+student.studentID);
    }
}
